/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2025-07-29 11:14:05
 * @LastEditors: zt 2993721164@qq.com
 * @LastEditTime: 2025-08-04 10:53:20
 * @FilePath: /yyshort/lib/common/apis/video_api.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:yyshort/common/model/video_model.dart';
import 'package:yyshort/common/utils/http.dart';
import 'package:yyshort/common/utils/json_storage_util.dart';

class VideoAPI {
  /// @description: 推荐视频列表
  /// @return {*} VideoListModel.list
  static Future for_you_videos() async {
    var response = await HttpUtil().post('/for_you_videos');
    // 保存数据
    try {
      await JsonStorageUtil.saveJson(
        response,
        key: 'for_you_videos', // 自定义键，方便后续读取
      );
      print('JSON数据保存成功');
    } catch (e) {
      print('保存失败: $e');
    }

    VideoListModel videoListModel = VideoListModel.fromJson(response);
    return videoListModel.list; //for search
    // return videoListModel; // for_you
  }

  /// @description: 搜索视频
  /// @param {Map} params 包含keywords和page参数的请求体
  /// @return {*} VideoListModel.list
  static Future search(String? keywords, String? page) async {
    Map<String, dynamic> params = {'keywords': keywords, 'page': page};

    var response = await HttpUtil().post('/search', data: params);
    VideoListModel videoListModel = VideoListModel.fromJson(response);
    return videoListModel.list;
  }

  /// @description: 获取视频详情
  /// @param {int} videoId 短剧ID（必填）
  /// @return {*} VideoModel
  static Future video_info(int videoId) async {
    // 构建form-data参数
    Map<String, dynamic> formData = {'video_id': videoId};
    var response = await HttpUtil().post('/video_info', data: formData);
    VideoModel videoModel = VideoModel.fromJson(response);
    return videoModel;
  }

  /// @description: 获取剧集内容
  /// @param {int} videoId 短剧ID（必填）
  /// @param {int} episodeId 剧集ID（必填）
  /// @return {*} EpisodeModel
  static Future video_episode_content(int videoId, int episodeId) async {
    // 构建form-data参数
    Map<String, dynamic> formData = {
      'video_id': videoId,
      'episode_id': episodeId,
    };
    var response = await HttpUtil().post(
      '/video_episode_content',
      data: formData,
    );
    if (response.containsKey('code')) {
      return response;
    } else {
      EpisodeModel episodeModel = EpisodeModel.fromJson(response);
      return episodeModel;
    }
  }

  /// @description: 添加或删除书架
  /// @param {int} videoId 短剧ID（必填）
  /// @return {*} Map 包含code和msg的响应
  static Future<bool> add_or_del_shelf(int videoId) async {
    // 构建form-data参数
    Map<String, dynamic> formData = {'video_id': videoId};
    Map<String, dynamic> response = await HttpUtil().post(
      '/add_or_del_shelf',
      data: formData,
    );
    if (response['code'] as int == 1) {
      return true;
    } else {
      return false;
    }
  }

  /// @description: 加入或删除喜欢
  /// @param {int} videoId 短剧ID（必填）
  /// @return {*} Map 包含code和msg的响应
  static Future<bool> add_or_del_like(int videoId) async {
    // 构建form-data参数
    Map<String, dynamic> formData = {'video_id': videoId};
    Map<String, dynamic> response = await HttpUtil().post(
      '/add_or_del_like',
      data: formData,
    );
    if (response['code'] as int == 1) {
      return true;
    } else {
      return false;
    }
  }
}
